﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using messageboard.model;
using messageboard.bll;
using System.IO;

namespace messageboard
{
    public partial class regist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MessageBll bll = new MessageBll();

            if (!IsPostBack)
            {
                for (int i = 1; i < 37; i++)
                {
                    DropDownList1.Items.Add(i.ToString());//为下拉按钮增加选项
                 }
            }
        }
     
        protected void Button1_Click(object sender, EventArgs e)
        {
            //构造一个User
            UserBll bll = new UserBll();
            string name = TextBox1.Text;
            string pw = TextBox2.Text;
            string email = TextBox3.Text;
            int age =  Convert.ToInt16(TextBox4.Text);
            string sex = ListBox1.SelectedValue;
            string face = DropDownList1.SelectedValue;
            User user = new User(0, name, pw, email, age, sex, face);


            if (IsValid)
            {   //判断用户名是否存在
                bool bname = bll.isexist(name);
                if (bname)
                {
                    
                    Response.Write("<script language='javascript'>alert('用户名已经存在');</script>");

                }

                else
                {   //添加用户
                    bool b = bll.regist(user);
                    if (b)
                    {
                        Session["name"] = name;
                       Session["id"]=bll.getid(name);
                    }

                    Response.Redirect("message.aspx");//定向到留言页面
                } 
            }

        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //为头像绑定SelectedIndexChanged事件，根据选择的序号显示相应头像
            string a = DropDownList1.SelectedValue;
            
            Image1.ImageUrl ="~/images/face/("+a+").png";
        }
     
       
        
    }
}